
# 将几个小的字符串合并为一个大的字符串
def demo_1():
    parts = ['Is', 'Chicago', 'Not', 'Chicago?']
    str_1 = ' '.join(parts)
    print(str_1)
    str_2 = ','.join(parts)
    print(str_2)

# 当使用加号+操作符去连接大量的字符串的时候效率非常低，因为加号连接会引起内存复制以及垃圾回收操作
def demo_2():
    a = 'Is Chicago'
    b = 'Not Chicago?'
    print(a + ' ' + b)
    print('{} {}'.format(a, b))
    c = 'hello' 'world'
    print(c)

# 利用生成器表达式转换数据为字符串同时合并字符串
def demo_3():
    data = ['ACME', 50, 91.1]
    s = ','.join(str(d) for d in data)
    print(s)

# 如果准备编写构建大量小字符串的输出代码，考虑使用生成器函数，利用yield
def sample():
    yield 'Is '
    yield 'Chicago '
    yield 'Not '
    yield 'Chicago?'

def demo_4():
    text = ''.join(sample())
    print(text)

if __name__ == '__main__':
    # demo_1()
    # demo_2()
    # demo_3()
    demo_4()